package com.example.javamaday.leetcode;

public class LeetCodeTest0165 {

    public static void main(String[] args) {
        new LeetCodeTest0165().compareVersion("1.101", "1.101.0");
    }

    public int compareVersion(String version1, String version2) {
        String[] splitV1 = version1.split("\\.");
        String[] splitV2 = version2.split("\\.");

        int lengthV1 = splitV1.length;
        int lengthV2 = splitV2.length;

        if (lengthV1 >= lengthV2) {
            for (int i = 0; i < lengthV1; i++) {
                if (i < lengthV2) {
                    if (Integer.parseInt(splitV1[i]) > Integer.parseInt(splitV2[i])) {
                        return 1;
                    } else if (Integer.parseInt(splitV1[i]) < Integer.parseInt(splitV2[i])) {
                        return -1;
                    } else {
                        continue;
                    }
                } else {
                    if (Integer.parseInt(splitV1[i]) > 0) {
                        return 1;
                    } else {
                        continue;
                    }
                }
            }
        } else {
            for (int i = 0; i < lengthV2; i++) {
                if (i < lengthV1) {
                    if (Integer.parseInt(splitV1[i]) > Integer.parseInt(splitV2[i])) {
                        return 1;
                    } else if (Integer.parseInt(splitV1[i]) < Integer.parseInt(splitV2[i])) {
                        return -1;
                    } else {
                        continue;
                    }
                } else {
                    if (Integer.parseInt(splitV2[i]) > 0) {
                        return -1;
                    } else {
                        continue;
                    }
                }
            }
        }
        return 0;
    }
}
